import React, { Component } from "react";

import { Popconfirm, message } from "antd";
import api from "../../../api/index";

/**
 * 确认框
 * @param {object} arg 相关配置
 */
export default function (props) {
  let {
    title,
    API,
    params,
    msg,
    self,
    tableData,
    callback,
    callbackParent,
    spinLoading,
    listData,
    total,
    respage,
    selectedFlag,
  } = props;

  function onConfirm() {
    spinLoading && self.setState({ spinLoading: true });
    console.log("api", API);
    console.log("params", params);
    api[API](params).then((res) => {
      let resData = window.$utils.checkJson(res);
      callbackParent && callbackParent();
      spinLoading && self.setState({ spinLoading: false });

      if (resData) {
        message.success(`${msg}`);
        if (listData) {
          let { API, listParams } = listData;
          self.setState({ loading: true }); //loading状态
          callback && callback();
          //操作成功，刷新列表数据
          spinLoading && self.setState({ spinLoading: true });
          api[API](listParams).then((res2) => {
            spinLoading && self.setState({ spinLoading: false });
            let data2 = window.$utils.checkJson(res2);
            if (data2) {
              self.setState({
                [tableData]: data2.list,
                loading: false,
              });
              // if (total && respage) {
              // 	self.setState({
              // 		total: res2.data.total,
              // 		respage: res2.data.page,
              // 		params: { ...self.state.params, pageNo: res2.data.page },
              // 		selectedFlag: false
              // 	});
              // }
            }
          });
        }
      }
    });
  }
  function onCancel() {
    message.warning(`您已取消操作`);
  }

  return (
    <Popconfirm
      title={title}
      onConfirm={onConfirm}
      onCancel={onCancel}
      cancelText="取消"
      okText="删除"
    >
      <a href="#">删除</a>

      {/* <a onClick={self.delF.bind(self, row)}>删除</a> */}
    </Popconfirm>
  );
}
